<template>
  <el-card>
    <template #header>
      <el-steps
        :active="activeStep"
        simple
      >
        <el-step
          v-for="(item, index) in steps"
          :key="index"
          :title="item.title"
          :icon="item.icon"
          @click="activeStep = index"
        />
      </el-steps>
    </template>
    <el-form label-width="80px">
      <div v-show="activeStep === 0">
        <el-form-item label="课程名称">
          <el-input v-model="course.courseName" />
        </el-form-item>
        <el-form-item label="课程简介">
          <el-input v-model="course.brief" />
        </el-form-item>
        <el-form-item label="课程概述">
          <el-input
            v-model="course.previewFirstField"
            style="margin-bottom: 10px"
            type="textarea"
            placeholder="概述1"
          />
          <el-input
            v-model="course.previewSecondField"
            type="textarea"
            placeholder="概述2"
          />
        </el-form-item>
        <el-form-item label="讲师姓名">
          <el-input v-model="course.teacherDTO.teacherName" />
        </el-form-item>
        <el-form-item label="讲师简介">
          <el-input v-model="course.teacherDTO.description" />
        </el-form-item>
        <el-form-item label="课程排序">
          <el-input-number
            v-model="course.sortNum"
            label="描述文字"
          />
        </el-form-item>
      </div>
      <div v-show="activeStep === 1">
        <el-form-item label="课程封面">
          <!--
            upload 上传文件组件，它支持自动上传，你只需要把上传需要参数配置一下就可以了
            -->
          <!--
            1. 组件需要根据绑定的数据进行图片预览
            2. 组件需要把上传成功的图片地址同步到绑定的数据中
            v-model 的本质还是父子组件通信
            1. 它会给子组件传递一个名字叫 value 的数据（Props）
            2. 默认监听 input 事件，修改绑定的数据（自定义事件）
            -->
          <course-image
            v-model="course.courseListImg"
            :limit="5"
          />
        </el-form-item>
        <el-form-item label="介绍封面">
          <course-image
            v-model="course.courseImgUrl"
            :limit="5"
          />
        </el-form-item>
      </div>
      <div v-show="activeStep === 2">
        <el-form-item label="售卖价格">
          <el-input
            v-model.number="course.discounts"
            type="number"
          >
            <template #append>
              元
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="商品原价">
          <el-input
            v-model.number="course.price"
            type="number"
          >
            <template #append>
              元
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="销量">
          <el-input
            v-model.number="course.sales"
            type="number"
          >
            <template #append>
              单
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="活动标签">
          <el-input v-model="course.discountsTag" />
        </el-form-item>
      </div>
      <div v-show="activeStep === 3">
        <el-form-item label="限时秒杀开关">
          <el-switch
            v-model="course.activityCourse"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
        <template v-if="course.activityCourse">
          <el-form-item label="开始时间">
            <el-date-picker
              v-model="course.activityCourseDTO.beginTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="结束时间">
            <el-date-picker
              v-model="course.activityCourseDTO.endTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="秒杀价">
            <el-input
              v-model.number="course.activityCourseDTO.amount"
              type="number"
            >
              <template #append>
                元
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="秒杀库存">
            <el-input
              v-model.number="course.activityCourseDTO.stock"
              type="number"
            >
              <template #append>
                个
              </template>
            </el-input>
          </el-form-item>
        </template>
      </div>
      <div v-if="activeStep === 4">
        <el-form-item label="课程详情">
          <text-editor v-model="course.courseDescriptionMarkDown" />
          <!-- <el-input v-model="course.courseDescriptionMarkDown" type="textarea"></el-input> -->
        </el-form-item>
        <el-form-item label="是否发布">
          <el-switch
            v-model="course.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSave"
          >
            保存
          </el-button>
        </el-form-item>
      </div>
      <el-form-item v-if="activeStep >= 0 && activeStep < 4">
        <el-button @click="activeStep++">
          下一步
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
import { defineComponent, onBeforeMount, ref } from 'vue'
import {
  saveOrUpdateCourse,
  getCourseById
} from '/@/services/course'
import CourseImage from './CourseImage.vue'
import TextEditor from '/@/components/TextEditor/index.vue'
import dayjs from 'dayjs'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
interface Course {
  [propName: string]: any
}
interface Props {
  isEdit: boolean
  courseId: string | number
}
let course:Course
const useInit = (props:Props) => {
  const router = useRouter()
  const activeStep = ref(0)
  course = ref({
    // id: 0,
    courseName: '',
    brief: '',
    teacherDTO: {
      // id: 0,
      // courseId: 0,
      teacherName: '',
      teacherHeadPicUrl: '',
      position: '',
      description: ''
    },
    courseDescriptionMarkDown: '',
    price: 0,
    discounts: 0,
    priceTag: '',
    discountsTag: '',
    isNew: true,
    isNewDes: '',
    courseListImg: '',
    courseImgUrl: '',
    sortNum: 0,
    previewFirstField: '',
    previewSecondField: '',
    status: 0, // 0：未发布，1：已发布
    sales: 0,
    activityCourse: false, // 是否开启活动秒杀
    activityCourseDTO: {
      // id: 0,
      // courseId: 0,
      beginTime: '',
      endTime: '',
      amount: 0,
      stock: 0
    },
    autoOnlineTime: ''
  })

  const loadCourse = async () => {
    const { data } = await getCourseById(props.courseId)
    const { activityCourseDTO } = data.data
    if (activityCourseDTO) {
      activityCourseDTO.beginTime = dayjs(activityCourseDTO.beginTime || '').format('YYYY-MM-DD')
      activityCourseDTO.endTime = dayjs(activityCourseDTO.endTime || '').format('YYYY-MM-DD')
    }
    course.value = data.data
  }

  const handleSave = async () => {
    const { data } = await saveOrUpdateCourse(course.value)
    if (data.code === '000000') {
      ElMessage.success('保存成功')
      router.push('/course')
    } else {
      ElMessage.error('保存失败')
    }
  }
  onBeforeMount(() => {
    if (props.isEdit) {
      loadCourse()
    }
  })
  return {
    handleSave,
    activeStep,
    course
  }
}
export default defineComponent({
  name: 'CreateOrUpdateCourse',
  components: {
    CourseImage,
    TextEditor
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [String, Number],
      default: null
    }
  },
  setup (props) {
    return {
      ...useInit(props),
      steps: [
        { title: '基本信息', icon: 'el-icon-edit' },
        { title: '课程封面', icon: 'el-icon-edit' },
        { title: '销售信息', icon: 'el-icon-edit' },
        { title: '秒杀活动', icon: 'el-icon-edit' },
        { title: '课程详情', icon: 'el-icon-edit' }
      ]
    }
  }
})
</script>

<style lang="scss" scoped>
.el-step {
  cursor: pointer;
}
</style>
